package com.twang.stock.service.jinyan;

import com.twang.stock.entity.*;
import com.twang.stock.mapper.HotTagMapper;
import com.twang.stock.mapper.StockInfoMapper;
import com.twang.stock.mapper.StockTagMapper;
import com.twang.stock.common.HotTagCommon;
import org.assertj.core.util.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
@Slf4j
public class HotTagService {


    @Resource
    private HotTagMapper hotTagMapper;

    @Resource
    private StockTagMapper stockTagMapper;
    @Resource
    private StockInfoMapper stockInfoMapper;
    @Autowired
    private HolidayService holidayService;

    public HotTag createHotTag(){
        String tag= HotTagCommon.getHotTag();
        HotTag hotTag=new HotTag();
        hotTag.setHotTag(tag);
       List<HotTag> list=hotTagMapper.select(hotTag);
       if(CollectionUtils.isEmpty(list)){
           hotTag.setCreateTime(new Date());
           hotTag.setUpdateTime(new Date());
           hotTagMapper.insertSelective(hotTag);
           return  hotTagMapper.select(hotTag).get(0);
       }else {
           return  list.get(0);
       }
    }

    public HotTag createHotTagByName(String tag){
        HotTag hotTag=new HotTag();
        hotTag.setHotTag(tag);
        List<HotTag> list=hotTagMapper.select(hotTag);
        if(CollectionUtils.isEmpty(list)){
            hotTag.setCreateTime(new Date());
            hotTag.setUpdateTime(new Date());
            hotTagMapper.insertSelective(hotTag);
            return  hotTagMapper.select(hotTag).get(0);
        }else {
            return  list.get(0);
        }
    }

    public  List<HotTag>  updateHotTag(HotTag hotTag){
        List<HotTag> hotTags= Lists.newArrayList();
        Example example=new Example(HotTag.class);
        if(null!=hotTag.getHotTag()){
            hotTags.add(hotTagMapper.selectByPrimaryKey(hotTag.getId()));
        }
        if(StringUtils.isNotBlank(hotTag.getHotTag())){
            example.createCriteria().andEqualTo("hotTag",hotTag.getHotTag());
            hotTags=hotTagMapper.selectByExample(example);
        }
        return hotTags;
    }

    public void inserStockTag(Long hottagId, Integer stockId, GaiNianZiJingPaiHang info){
        if(hottagId==null||stockId==null){
            return;
        }
        StockTag stockTag=new StockTag();
        stockTag.setStockId(stockId);
        stockTag.setTagId(hottagId);
        stockTag.setCreateTime(new Date());
        stockTag.setHangYe(info.getHanYe());
        stockTag.setHangYeId(info.getId());
        stockTag.setUpdateTime(new Date());
        stockTagMapper.insert(stockTag);
    }

    public void inserStockTagFor精选(Long hottagId, Integer stockId) {
        if (hottagId == null || stockId == null) {
            return;
        }
        StockTag param = new StockTag();
        param.setStockId(stockId);
        param.setTagId(hottagId);
        StockTag stockTagRecord = stockTagMapper.selectOne(param);
        if (null != stockTagRecord) {
            return;
        } else {
            StockTag stockTag = new StockTag();
            stockTag.setStockId(stockId);
            stockTag.setTagId(hottagId);
            stockTag.setCreateTime(new Date());
            stockTag.setHangYe("精选");
            stockTag.setHangYeId(null);
            stockTag.setUpdateTime(new Date());
            stockTagMapper.insert(stockTag);
        }
    }

    /**
     * g更新热点温度
     */
    public void getHotTemperatureName(){
        Example example=new Example(HotTag.class);
        example.createCriteria().andIsNull("hotTemperature").andLike("hotTag","%精选%");
        List<HotTag> list=hotTagMapper.selectByExample(example);
        //获取stickId
        for(HotTag hotTag:list){
            List<String> stockCodes= stockTagMapper.getStockIdsByHotTagIds(hotTag.getId());
            BigDecimal hotTemperature=BigDecimal.ZERO;
            if(!CollectionUtils.isEmpty(stockCodes)){
                 HotTag hotTag1=   hotTagMapper.selectNextBytagId(hotTag.getId());
                 if(null==hotTag1){
                     continue;
                 }
                 String date=hotTag1.getHotTag().split("-")[1];
                Example exampleStockInfo=new Example(StockInfo.class);
                exampleStockInfo.createCriteria().andIn("code",stockCodes).andEqualTo("date",date);
                List<StockInfo> stockInfoList=  stockInfoMapper.selectByExample(exampleStockInfo);
                if(!CollectionUtils.isEmpty(stockInfoList)) {
                    hotTemperature = stockInfoList.stream().map(detail -> Optional.ofNullable(detail.getZhangdie()).orElse(BigDecimal.ZERO)).reduce(BigDecimal.ZERO, BigDecimal::add);
                    hotTemperature = hotTemperature.divide(BigDecimal.valueOf(stockInfoList.size()), 2, BigDecimal.ROUND_HALF_UP);
                }
            }
            hotTag.setHotTemperature(hotTemperature.floatValue());
            hotTagMapper.updateByPrimaryKey(hotTag);
        }
    }

}
